import API from '../api';
import {md5} from '../util'

let api
export default class Code{
	constructor(options){
		api = new API(options)
	}
	fetch(...args){
		return api.genFeatureMod('user')(...args)
	}
	refreshToken = async (usermark, token) => {
		const param = {
			act: 'refresh',
			user_refresh_token: token
		}
		if(parseInt(usermark) == usermark){
			param.user_id = usermark
		} else if(~usermark.indexOf('@')){
			param.user_mail = usermark
		} else {
			param.user_name = usermark
		}

		const res = await this.fetch(param)
		return res;
	}

	edit = async data => {
		const param = {
			act: 'info',
			...data
		}
		if(param.user_pass){
			param.user_pass = md5(param.user_pass);
		}
		if(param.user_access_token){
			param.user_access_token = md5(param.user_access_token);
		}

		const res = await this.fetch(param)
		return res;
	}

	resetPassword = async data => {
		const param = {
			act: 'edit',
			...data
		}
		if(param.user_pass){
			param.user_pass = md5(param.user_pass);
		}
		if(param.user_access_token){
			param.user_access_token = md5(param.user_access_token);
		}

		const res = await this.fetch(param)
		return res;

	}
}